<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.bootcss.com/vue/2.5.15/vue.min.js"></script>
  </head>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    .divout {
      width: 100px;
      height: 100px;
      background: yellow;
      margin-bottom: 50px;
      border: 1px solid black;
    }
    .divout1 {
      width: 50px;
      height: 50px;
      background: yellow;
      margin-top: 80px;
      /*float:left;*/
      /*position:absolute;*/
      border: 1px solid black;
    }
  </style>
  <body>
    <div>数组扁平化</div>
    <div class="divout"></div>
    <div class="divout1"></div>
  </body>
  <script>
    var app = new Vue({
      el: "#app",
      data: {
        arr: [
          // 需要按顺序
          { id: 4, name: "部门4", pid: 3 },
          { id: 5, name: "部门5", pid: 4 },
          { id: 1, name: "部门1", pid: 0 },
          { id: 2, name: "部门2", pid: 1 },
          { id: 3, name: "部门3", pid: 1 },
        ]
      },
      // 关于执行顺序，敢于进行改造
      async mounted() {
        // console.log(result);
        await this.process([1,2,3])
        await this.process1([4,5,6])
        await this.process2([7,8,9])
        console.log('end')
      },
      methods: {
        async process(array) {
          for (const i of array) {
            await this.doSomething(i);
          }
        },
        async process1(array) {
          array.forEach(async i => {
            await this.doSomething(i);
          });
        },
        async process2(array) {
          for await (const i of array) {
            await this.doSomething(i);
            // this.doSomething(i);
          }
        },
        doSomething (i) {
          return new Promise((resolve, reject) => {
            setTimeout(() => {
              let box = [1, 2, 3]
              console.log('doSomething', i)
              resolve(box)
            }, 500);
          })
        }
      },
    });
  </script>
</html>
